package ua.org.plast.mobileApp.news;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import ua.org.plast.mobileApp.App;
import ua.org.plast.mobileApp.R;
import ua.org.plast.mobileApp.fragments.BaseFragment;
import ua.org.plast.mobileApp.news.tools.Article;
import ua.org.plast.mobileApp.news.tools.RSSHandler;
import ua.org.plast.mobileApp.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Virus1908
 * Date: 08.11.13
 * Time: 16:35
 */
public class NewsFragment extends BaseFragment {
    private static final String[] resources = {"http://www.plast.org.ua/rss.php?rssfeedid=3"};
    ListView mList;

    @Override
    public String getActivityLabel() {
        return App.getContext().getResources().getString(R.string.nav_news);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View returnView = inflater.inflate(R.layout.news_fragment, null);
        mList = (ListView) returnView.findViewById(R.id.news_list);
        final ProgressDialog progressDialog = new ProgressDialog(getActivity());
        AsyncTask<Object, Object, List<Article>> task = new AsyncTask<Object, Object, List<Article>>() {
            @Override
            protected void onPreExecute() {
                progressDialog.show();
                progressDialog.setMessage("етап 1(завантаження рсс)");
            }

            @Override
            protected List<Article> doInBackground(Object... objects) {
                RSSHandler handler = new RSSHandler();
                List<Article> l = handler.getLatestArticles(resources[0]);
                return l;
            }

            @Override
            protected void onPostExecute(final List<Article> articles) {
                progressDialog.setMessage("етап 2(завантаження контенту)");
                if (articles != null) {
                    ImageUtils utils = new ImageUtils();
                    utils.setOnLoadListener(new ImageUtils.OnLoadListener() {
                        @Override
                        public void onLoad() {
                            progressDialog.hide();
                            mList.setAdapter(new NewsListAdapter(getActivity(), articles));
                        }
                    });
                    for (Article article : articles) {
                        utils.loadBitmapAsync(article.getImgLink());
                    }
                    utils.allowLoadListener();
                } else {
                    progressDialog.hide();
                    Toast.makeText(getActivity(), " немає статей або виникла помилка", Toast.LENGTH_SHORT).show();
                }

            }
        };

        task.execute();
        return returnView;
    }
}
